define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            $.fn.bootstrapTable.locales[Table.defaults.locale]['formatSearch'] = function(){return "搜索SN号";};
            Table.api.init({
                extend: {
                    index_url: 'transaction/index' + location.search,
                    // add_url: 'transaction/add',
                    // edit_url: 'transaction/edit',
                    // del_url: 'transaction/del',
                    multi_url: 'transaction/multi',
                    import_url: 'transaction/import',
                    table: 'transaction_data_xyf',
                }
            });

            $('.panel-heading a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var value = $(this).data("value");
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                var queryParams = options.queryParams;
                options.queryParams = function (params) {
                    params = queryParams(params);
                    params.product = value;
                    return params;
                };
                return false;
            });

            var table = $("#table");

            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                var sum = data.sum;
                var html = "交易总额:<span>【"+sum+"】</span>";
                $(".extend").html(html);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'sn', title: __('Sn'), operate: 'LIKE'},
                        {field: 'order_num', title: __('Order_num'), operate: 'LIKE'},
                        {field: 'merchant_name', title: __('Merchant_name'), operate: 'LIKE'},
                        {field: 'transaction_amount', title: __('Transaction_amount'), operate:false},
                        {field: 'transaction_rate', title: __('Transaction_rate'), operate:false},
                        {field: 'trading_time', title: __('Trading_time'),defaultValue: Controller.api.today(0) + " 00:00:00 - " + Controller.api.today(0) + ' 23:59:59', operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            today: function (AddDayCount) {
                const dd = new Date();
                dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期
                let y = dd.getFullYear();
                let m = dd.getMonth()+1;//获取当前月份的日期
                let d = dd.getDate();
                //判断月
                if (m < 10) {
                    m = "0" + m;
                }
                //判断日
                if (d < 10) {
                    d = "0" + d;
                }
                return y + "-" + m + "-" + d;
            }
        }
    };
    return Controller;
});
